package Server;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import java.sql.*;
import Server.*;
import ocsf.server.AbstractServer;
import ocsf.server.ConnectionToClient;

public class login {
	
	

	
	public static int chackvalidation(Connection con1,String username, String password )
	{
		int state=0;
		 try
         {
             Statement stmt = con1.createStatement();
            java.sql.PreparedStatement ps = con1.prepareStatement("SELECT * FROM clients WHERE Name= ? and Password= ?;");
             ps.setObject(1, username);
             ps.setObject(2, password);
          // state:1=incorrect pass ; 2=user does not exist ; 3=user already
     		// logged ; 4=success
             ResultSet res = stmt
				.executeQuery("SELECT * FROM users WHERE username='"
						+ username + "'");
             
             if (res.next()) {
     			if (!(res.getString(3).equals(password)))
     				state=1;
     			else if (res.getBoolean(4) == true)
     				state=3;
     			else {
     				stmt.executeUpdate("UPDATE users SET logged=1 WHERE name='"
     						+ username + "'");
     				state=4;
     			}
     		} else
     			state=2;
     		res.close();
     	} catch (SQLException e) {
     		e.printStackTrace();
     	}
		
		
		return state;
		
	}


	
	/*
	Login log = (Login) msg;
	try {

		stmt = con1.createStatement();
		// state:1=incorrect pass ; 2=user does not exist ; 3=user already
		// logged ; 4=success
		ResultSet res = stmt
				.executeQuery("SELECT * FROM users WHERE name='"
						+ log.getUserlogin() + "'");
		if (res.next()) {
			if (!(res.getString(2).equals(log.getPasswordlogin())))
				log.setState(1);
			else if (res.getBoolean(3) == true)
				log.setState(3);
			else {
				stmt.executeUpdate("UPDATE users SET loged=1 WHERE name='"
						+ log.getUserlogin() + "'");
				log.setState(4);
			}
		} else
			log.setState(2);
		res.close();
	} catch (SQLException e) {
		e.printStackTrace();
	}
	return log;
}
	
	*/
}
